package cn.edu.bzu.ie.sf.mealsmisweb.mapper;

import cn.edu.bzu.ie.sf.mealsmisweb.entity.Account;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * @version v1.0.0
 * @Package : cn.edu.bzu.ie.sf.mealsmisweb.mapper
 * @Description : 伙食账目公布表
 * @Create on : 2021/8/18 17:18
 * @author: Feng Song 宋锋
 **/
@Repository
public interface AccountMapper extends Mapper<Account> {
    /**
     * 取得某个月份下的，某的你部门的所有的子部门的伙食账目公布表
     * @param pid 父部门
     * @param month 月份
     * @return List<Account> 集合实例
     */
    @Select("select a.*,b.uname " +
            " from account a " +
            " inner join userinfo b on a.uid=b.id " +
            " inner join department c on a.depid=c.id " +
            " where c.pid=#{pid} and a.month=#{month} ")
    List<Account> getAllAccountListByPidAndMonth(int pid, String month);

    /**
     * 某个所有账目公布表的数量
     * @param depid 部门
     * @return long 记录条数
     */
    @Select("select count(1) as sl from account")
    long getCountByDepid(int depid);

    /**
     * 获取某个部门下的所有账目公布表分页信息
     * @param depid 部门
     * @param firstNum 页面中的第1条记录
     * @param pageSize 每个分页的大小
     * @return List<Account> 集合实例
     */
    @Select("select a.*, b.uname" +
            " from account a" +
            " inner join userinfo b on a.uid=b.id "+
            " where a.depid=#{depid} order by a.id desc limit #{firstNum}, #{pageSize}")
    List<Account> getAccountPageListByDepid(int depid, int firstNum, int pageSize);

    /**
     * 取得某个部门某个月份的账目公布表信息
     * @param depid 部门
     * @param month 月份
     * @return Account 实例
     */
    @Select("select a.*, b.uname from account a inner join userinfo b on a.uid=b.id where a.depid=#{depid} and a.month=#{month} ")
    Account getAccoutByDepidAndMonth(int depid, String month);

    /**
     * 添加账目信息
     * @param item 要添加的账目，无id
     * @return int 影响的行数，有id
     */
    @Insert("insert into account(uid,depid, pic, month, create_dt) values(#{uid}, #{depid}, #{pic}, #{month}, #{create_dt})")
    @Options(useGeneratedKeys=true, keyProperty="id")
    int addAccount(Account item);

    /**
     * 修改账目信息
     * @param item 要修改的账目信息
     * @return int 影响的行数
     */
    @Update("update account set pic=#{pic}, create_dt=#{create_dt} where id=#{id}")
    int editAccount(Account item);

    /**
     * 删除账目信息
     * @param id 要删除账目的id
     * @return int 影响的行数
     */
    @Delete("delete from account where id=#{id}")
    int delAccount(int id);
}
